﻿#region "Copyright (C) Lenny Granovsky. 2005-2013"
//This program is free software distribute under the terms of the GNU General Public License as published by
//the Free Software Foundation. Please read AssemblyInfo.cs file for more information.
#endregion

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Reflection;
using System.Web.Services.Protocols;
using System.Web;
using System.Web.Caching;

namespace Web.Enhancements.Rest.InfoCaching
{
    [Serializable()]
    internal class JSClassCache : Dictionary<string, string>
    {
        public const string MemorySlotName = "WebRestJSClassCache";

        public static JSClassCache Current
        {
            get
            {
                object tmp = null;
                if (WebRestSettings.Current.InfoCacheLocation == ClassMethodInfoCacheLocation.ApplicationCache)
                {
                    tmp = HttpRuntime.Cache.Get(MemorySlotName);
                    if (tmp == null)
                    {
                        tmp = new JSClassCache();
                        HttpRuntime.Cache.Insert(MemorySlotName, tmp, null, Cache.NoAbsoluteExpiration, Cache.NoSlidingExpiration, CacheItemPriority.NotRemovable, null);
                    }
                }
                else
                {
                    tmp = HttpContext.Current.Application[MemorySlotName];
                    if (tmp == null)
                    {
                        tmp = new JSClassCache();
                        HttpContext.Current.Application[MemorySlotName] = tmp;
                    }
                }
                return (JSClassCache)tmp;
            }
            set
            {
                if (WebRestSettings.Current.InfoCacheLocation == ClassMethodInfoCacheLocation.ApplicationCache)
                    HttpRuntime.Cache.Insert(MemorySlotName, value, null, Cache.NoAbsoluteExpiration, Cache.NoSlidingExpiration, CacheItemPriority.NotRemovable, null);
                else
                    HttpContext.Current.Application[MemorySlotName] = value;
            }
        }

        public JSClassCache()
            : base()
        {
        }

        public static string CreateKey(string classFullName)
        {
            return classFullName.ToLower();
        }
    }
}
